/************************************************************************
 * File: player.cpp                                                     *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#include "player.h"
#include "tile.h"
#include <QString>
#include <QPainter>
#include "company.h"

Player::Player(const QString &name, bool isComputer, QWidget *parent)
	: QWidget(parent)
{
    m_name = name;
    m_isComputer = isComputer;
	m_points = 0;
	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
}

int Player::getMoney() const
{
    return m_money;
}

int Player::nShares(Company *company) const
{
    return m_shares[company];
}

bool Player::isMajority(Company *company) const
{
    return m_isMajority[company];
}

bool Player::isMinority(Company *company) const
{
    return m_isMinority[company];
}

int Player::nTiles() const
{
    return m_tiles.size();
}

const QString &Player::getName() const 
{
    return m_name;
}

const Tile &Player::getTile(int tile) const
{
    return m_tiles.at(tile);
}

int Player::getBuyLimit() const
{
    return m_buyLimit;
}

bool Player::isComputer() const
{
    return m_isComputer;
}

int Player::getAiDesire(Company *company) const
{
    return m_aiDesires[company];
}

QSize Player::sizeHint() const
{
	return QSize(400, 20);
}

bool Player::hasTile(const Tile &tile) const
{
	return m_tiles.contains(tile);
}

QListIterator<Tile> Player::getTileIterator() const
{
	return QListIterator<Tile>(m_tiles);
}

bool Player::canSell(Company *company) const
{
	return nShares(company) > 0;
}

bool Player::canTrade(Company *big, Company *small) const
{
	return nShares(small) > 1 && big->nShares() > 0;
}

void Player::reset(const QList<Company*> &companies)
{
    m_tiles.clear();
    m_shares.clear();
    m_isMajority.clear();
    m_isMinority.clear();
	m_aiDesires.clear();
	m_money = 0;
	m_highlight = None;
	m_keyOrder.clear();
	QListIterator<Company*> i(companies);
	while (i.hasNext()) {
		m_keyOrder.append(i.next());
		m_shares[m_keyOrder.last()] = 0;
	}
	resetAiDesire();
	update();
}

void Player::giveTile(const Tile &tile)
{
    m_tiles.push_back(tile);
}

void Player::setBuyLimit(int limit) 
{
    m_buyLimit = limit;
}

void Player::removeTile(const Tile &tile)
{
    m_tiles.removeAll(tile);
}

void Player::setMajority(Company *company, bool isMajority)
{
	if (m_isMajority[company] != isMajority) {
		m_isMajority[company] = isMajority;
		update();
	}
}

void Player::setMinority(Company *company, bool isMinority)
{
	if (m_isMinority[company] != isMinority) {
		m_isMinority[company] = isMinority;
		update();
	}
}

void Player::giveShares(Company *company, int amount)
{
    m_shares[company] += amount;
	update();
}

void Player::takeShares(Company *company, int amount)
{
    m_shares[company] -= amount;
	update();
}

void Player::giveMoney(int amount)
{
	m_money += amount;
	update();
}

void Player::takeMoney(int amount)
{
	m_money -= amount;
	update();
}

void Player::setAiDesire(Company *company, int value)
{
    m_aiDesires[company] = value;
}

void Player::adjustAiDesire(Company *company, int amount)
{
    m_aiDesires[company] += amount;
}

void Player::resetAiDesire()
{
	QListIterator<Company*> i(m_keyOrder);
	while (i.hasNext()) {
		setAiDesire(i.next(), 0);
	}
}

void Player::givePoints(int amount)
{
	m_points += amount;
}

int Player::getPoints() const
{
	return m_points;
}

void Player::setHighlight(Highlight highlight)
{
	if (m_highlight != highlight) {
		m_highlight = highlight;
		update();
	}
}

void Player::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	
	QLinearGradient gradient(rect().topLeft(), rect().bottomRight());
	if (m_highlight == Merge) {
		gradient.setColorAt(0.0, QColor(164, 64, 64));
		gradient.setColorAt(1.0, Qt::darkGray);
	} else if (m_highlight == Turn) {
		gradient.setColorAt(0.0, QColor(128, 96, 64));
		gradient.setColorAt(1.0, Qt::darkGray);
	} else if (m_highlight == DimTurn) {
		gradient.setColorAt(0.0, QColor(128, 128, 96));
		gradient.setColorAt(1.0, Qt::darkGray);
	} else {
		gradient.setColorAt(0.0, QColor(64, 64, 64));
		gradient.setColorAt(1.0, Qt::darkGray);
	}
	
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(gradient));
	painter.drawRoundedRect(rect(), 10, 10);
	
	painter.setPen(Qt::white);
	painter.setFont(QFont("Helvetica", 12, QFont::Bold));
	painter.drawText(5, 15, getName());
	painter.drawText(100, 15, QString("$") + QString::number(getMoney()));
	
	
	int width = (rect().width() - 200) / m_shares.size();	
	QListIterator<Company*> i(m_keyOrder);
	int x = 0;
	while (i.hasNext()) {
		Company *key = i.next();
		if (nShares(key) > 0) {
			QRect box(200 + (x * width), 2, 15, 15);
			QLinearGradient grad(box.topLeft(), box.bottomRight());
			grad.setColorAt(0.0, key->getColor().lighter());
			grad.setColorAt(1.0, key->getColor().darker());
			painter.setBrush(QBrush(grad));
			painter.setPen(Qt::NoPen);
			painter.drawRoundedRect(box, 3, 3);
			
			QString text = QString::number(nShares(key));
			if (isMajority(key)) {
				text += "+";
			} else if (isMinority(key)) {
				text += "-";
			}
			box.adjust(20, 0, 40, 0);
			painter.setPen(Qt::white);
			painter.drawText(box, Qt::AlignVCenter | Qt::AlignLeft, text);
		}
		x++;
	}
}